<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form :model="queryParams" :inline="true">
            <el-form-item label="订单号" prop="cardOrderNumber">
              <el-input v-model="queryParams.cardOrderNumber" placeholder="请输入订单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="三方单号 " prop="threePaymentNumber">
              <el-input v-model="queryParams.threePaymentNumber" placeholder="请输入单号 " clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="sendPhone">
              <el-input v-model="queryParams.sendPhone" placeholder="请输入收货手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户昵称" prop="nickName">
              <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="店铺名" prop="shopName">
              <el-input v-model="queryParams.shopName" placeholder="请输入店铺名" clearable @keyup.enter="handleQuery" :disabled="true"/>
            </el-form-item>
            <el-form-item label="拼团编码" prop="collageCode" >
              <el-input v-model="queryParams.collageCode" placeholder="请输入拼团编码" clearable :disabled="true"/>
            </el-form-item>
            <el-form-item label="支付方式" prop="payWay">
              <el-select v-model="queryParams.payWay" placeholder="请选择方式" clearable @keyup.enter="handleQuery">
                <el-option v-for="dict in pay_way" :key="dict.value" :value="dict.value" :label="dict.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="玩家号" prop="userInviteCode">
              <el-input v-model="queryParams.userInviteCode" placeholder="请输入玩家号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <div style="margin-top: -15px;">
        <el-tabs v-model="queryParams.cardOrderStatus" type="card" @tab-change="handleQuery">
          <el-tab-pane label="全部" name=""></el-tab-pane>
          <el-tab-pane label="待付款" name="1"></el-tab-pane>
          <el-tab-pane label="已付款" name="2"></el-tab-pane>
          <el-tab-pane label="未中卡" name="3"></el-tab-pane>
          <el-tab-pane label="已中卡" name="4"></el-tab-pane>
          <el-tab-pane label="已取消" name="5"></el-tab-pane>
          <el-tab-pane label="退款中" name="6"></el-tab-pane>
          <el-tab-pane label="已退款" name="7"></el-tab-pane>
        </el-tabs>
      </div>
      <div class="hidden-table-header" style="background: #F0F0F0">
        <el-table :row-class-name="'hidden-row'" border :data="[{}]" :show-header="true"
                  :header-cell-style="{ background: '#fafafa' }" style="width: 100%;" @tab-change="handleQuery">
          <el-table-column label="商品信息" align="center" min-width="20%" />
          <el-table-column label="玩家信息" align="center" min-width="15%" />
          <el-table-column label="单价/数量" align="center" min-width="8%" />
          <el-table-column label="订单金额" align="center" min-width="8%" />
          <el-table-column label="收货信息" align="center" min-width="15%" />
          <el-table-column label="订单来源" align="center" min-width="5%" />
          <el-table-column label="操作" align="center" min-width="8%" />
        </el-table>
      </div>
      <div>
        <div v-for="(cardOrder, index) in orderList" :key="index">
          <div class="main-order-header">
            <div style="margin-left: 10px">订单号：<span type="primary" style="color:#1a4fa9;"> {{
                cardOrder.cardOrderNumber }} </span></div>
            <div style="margin-left: 20px"><dict-tag :options="card_order_status" :value="cardOrder.cardOrderStatus" /></div>
            <div style="margin-left: 20px"><el-tag>下单时间：<span>{{ cardOrder.createTime }}</span></el-tag></div>
            <div style="margin-left: 20px" v-if="cardOrder.payTime"><el-tag type="success">支付时间：<span>{{ cardOrder.payTime
              }}</span></el-tag></div>
            <div style="margin-left: 20px" v-if="cardOrder.refundTime"><el-tag type="info">退款时间：<span>{{
                cardOrder.refundTime }}</span></el-tag></div>
          </div>
          <div>
            <el-table border :show-header="false" :data="[cardOrder]" style="width: 100%"
                      :header-cell-style="{ background: '#F0F0F0' }">
              <el-table-column label="商品信息" align="center" min-width="20%">
                <template #default="scope">
                  <div>
                    <el-row :gutter="24" style="align-items: center;">
                      <el-col :span="12">
                        <image-preview :src="scope.row.collageImagePath" width="130px" height="90px"></image-preview>
                      </el-col>
                      <el-col :span="12" style="margin-left: -20px;">
                        <el-text  :line-clamp='4' style="color: dodgerblue; text-align: auto"> {{ scope.row.collageName }} </el-text>
                      </el-col>
                    </el-row>
                  </div>
                  <div>
                    <span style="color: #945dd9;">编码:  {{ scope.row.collageCode }} </span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="玩家号" align="center" min-width="15%">
                <template #default="scope">
                  <el-row :gutter="24">
                    <el-col :span="10">
                      <image-preview :src="scope.row.appUser.headPortrait" width="100px" height="100px"></image-preview>
                    </el-col>
                    <el-col :span="14">
                      <div>
                        <span v-if="scope.row.appUser">玩家号: {{ scope.row.appUser.inviteCode }}</span>
                      </div>
                      <div style="margin-top: 25px">
                        <span v-if="scope.row.appUser">昵称: {{ scope.row.appUser.nickName }}</span>
                      </div>
                    </el-col>
                  </el-row>
                </template>
              </el-table-column>
              <el-table-column label="单价/数量" align="center" min-width="8%">
                <template #default="scope">
                  <div>
                    <span style="color: dodgerblue">商品单价：  {{ scope.row.unitPrice }}</span>
                  </div>
                  <div style="margin-top: 10px">
                    <span style="color: #E65D6E">购买份数： x {{ scope.row.count }}</span>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="订单金额" align="center" prop="orderPrice" min-width="8%">
                <template #default="scope">
                  <div>
                    <span style="color: red">订单总价:{{ scope.row.orderAmount }}</span>
                  </div>
                  <div v-if="scope.row.firtstCheapAmount && scope.row.firtstCheapAmount > 0" style="margin-top: 5px">
                    <span style="color: #0887cb">首单优惠:{{ scope.row.firtstCheapAmount }}</span>
                  </div>
                  <div v-if="scope.row.discountsAmount && scope.row.discountsAmount > 0" style="margin-top: 5px">
                    <span style="color: #945dd9">优惠券:{{ scope.row.discountsAmount }}</span>
                  </div>
                  <div v-if="scope.row.fullReduceAmount && scope.row.fullReduceAmount > 0" style="margin-top: 5px">
                    <span style="color: orange">满减:{{ scope.row.fullReduceAmount}}</span>
                  </div>
                  <div style="margin-top: 5px">
                    <span style="color: lightseagreen">实付金额:{{ scope.row.actualPayment }}</span>
                  </div>
                  <div style="margin-top: 5px" v-if="scope.row.payWay">
                    <div v-if="scope.row.payWay == '9' ">
                      <el-popover placement="bottom" :content="scope.row.payeeSubMchId + ',球星卡1' " :width="220" trigger="hover" v-if="scope.row.payeeSubMchId == '67cc593844d40bae1096459c' ">
                        <template #reference>
                          <dict-tag :options="pay_way" :value="scope.row.payWay" />
                        </template>
                      </el-popover>
                      <el-popover placement="bottom" :content="scope.row.payeeSubMchId + ',球星卡2' " :width="220" trigger="hover" v-else-if="scope.row.payeeSubMchId == '67cc5c4b44d40bae1096459d' ">
                        <template #reference>
                          <dict-tag :options="pay_way" :value="scope.row.payWay" />
                        </template>
                      </el-popover>
                      <el-popover placement="bottom" :content="scope.row.payeeSubMchId + ',卡领' " :width="220" trigger="hover" v-else-if="scope.row.payeeSubMchId == '67e64fec44d46e5b5defdcd4' ">
                        <template #reference>
                          <dict-tag :options="pay_way" :value="scope.row.payWay" />
                        </template>
                      </el-popover>
                    </div>
                    <div v-else>
                      <dict-tag :options="pay_way" :value="scope.row.payWay" />
                    </div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="收货信息" header-align="center" align="left" min-width="15%">
                <template #default="scope">
                  <div>
                    收货人：{{ scope.row.sendName }}
                  </div>
                  <div style="margin-top: 5px">
                    手机号：{{ scope.row.sendPhone }}
                  </div>
                  <div style="margin-top: 5px">
                    地址：{{ scope.row.sendProvince }}{{ scope.row.sendCity }}{{ scope.row.sendArea }}{{ scope.row.detailAddress }}
                  </div>
                </template>
              </el-table-column>
              <!--              <el-table-column label="物流信息" header-align="center" align="left" min-width="15%"-->
              <!--                               v-if="queryParams.selectType == '4'">-->
              <!--                <template #default="scope">-->
              <!--                  <div v-if="scope.row.ordersExpressageList && scope.row.ordersExpressageList.length > 0">-->
              <!--                    <el-card v-for="(ordersExpressage, index) in scope.row.ordersExpressageList" :key="index"                         style="margin-bottom: 5px">-->
              <!--                      <div>-->
              <!--                        物流公司：{{ ordersExpressage.expressageCompany }}-->
              <!--                      </div>-->
              <!--                      <div style="margin-top: 5px">-->
              <!--                        物流单号：{{ ordersExpressage.expressageNumber }}-->
              <!--                      </div>-->
              <!--                    </el-card>-->
              <!--                  </div>-->
              <!--                </template>-->
              <!--              </el-table-column>-->
              <el-table-column label="订单来源 " align="center" prop="orderSource" min-width="5%" >
                <template #default="scope">
                  <div>
                    <dict-tag :options="order_source" :value="scope.row.orderSource" />
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="8%">
                <template #default="scope">
                  <div>
                    <el-button type="text" icon="View" @click="handleView(scope.row)">订单详情</el-button>
                  </div>
                  <div style="margin-top: 5px">
                    <el-button
                               v-if="scope.row.cardCollageStatus == '6' && (scope.row.cardOrderStatus == '3' || scope.row.cardOrderStatus == '2')"
                               type="text" icon="Postcard" @click="viewListCarmi(scope.row, '查看卡密')">查看卡密</el-button>
                  </div>
                  <div style="margin-top: 5px">
                    <el-button
                               v-if="scope.row.cardOrderStatus == '4' "
                               type="text" icon="CreditCard" @click="viewListCarmi(scope.row, '查看中卡')">查看中卡</el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="getList" />

    </el-card>

    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="orderFormRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="订单详情" v-model="orderDialog.visible" width="1200px" append-to-body :destroy-on-close="true">
      <cardInfoDiaLog :cardOrderId="cardOrderId" />
    </el-dialog>

    <el-dialog :title="carmiDialog.title" top="3vh" v-model="carmiDialog.visible" width="1360px" append-to-body :destroy-on-close="true">
      <carmiDiaLog :cardOrderId="cardOrderId" :cardCollageId="cardCollageId" />
    </el-dialog>

    <el-dialog :title="exclusiveCarmiDialog.title" top="3vh" v-model="exclusiveCarmiDialog.visible" width="1260px" append-to-body :destroy-on-close="true">
      <exclusiveCarmiDiaLog :cardOrderId="cardOrderId" :cardCollageId="cardCollageId" />
    </el-dialog>

  </div>
</template>

<script lang="ts" setup>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from '@/api/system/cardOrder';
import { OrderVO, OrderQuery, OrderForm } from '@/api/system/cardOrder/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
import cardInfoDiaLog from '@/components/cardOrder/cardInfo.vue';
import carmiDiaLog from '@/components/cardOrder/carmiInfo.vue';
import exclusiveCarmiDiaLog from '@/components/cardOrder/exclusiveCarmiInfo.vue';
const { card_order_status, open_or_close, sys_yes_no, pay_way,order_source } = toRefs<any>(proxy?.useDict('card_order_status', 'order_source', 'open_or_close','sys_yes_no', 'pay_way'));

const orderList = ref<OrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const cardOrderId = ref();
const cardCollageId = ref();
const collageCode = ref();

const queryFormRef = ref<ElFormInstance>();
const orderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const orderDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const carmiDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const exclusiveCarmiDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: OrderForm = {
}
const data = reactive<PageData<OrderForm, OrderQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: undefined,
    cardOrderStatus: '',
    cardOrderNumber: undefined,
    selectTimeType: undefined,
    unitPrice: undefined,
    count: undefined,
    orderPrice: undefined,
    orderFreight: undefined,
    threePaymentNumber: undefined,
    sendProvince: undefined,
    sendCity: undefined,
    sendArea: undefined,
    detailAddress: undefined,
    sendName: undefined,
    sendPhone: undefined,
    nickName: undefined,
    cancelTime: undefined,
    payTime: undefined,
    shipTime: undefined,
    receivedTime: undefined,
    refundTime: undefined,
    shopId: undefined,
    cardCollageId: undefined,
    collageImagePath: undefined,
    collageCode: undefined,
    cardCollageName: undefined,
    playWay: undefined,
    payWay: undefined,
    actualPayment: undefined,
    discountsOriginAmount: undefined,
    discountsAmount: undefined,
    discountsGetLogId: undefined,
    discountsIssueType: undefined,
    fullReduceAmount: undefined,
    isGainCard: undefined,
    payBeanId: undefined,
    userAdressId: undefined,
    userInviteCode: undefined,
    receivedWay: undefined,
    userAnonymous: undefined,
    boxPosition: undefined,
    subMchid: undefined,
    isShare: undefined,
    shareRatio: undefined,
    shareMoney: undefined,
    extraShareMoney: undefined,
    shareOrderId: undefined,
    shareStatus: undefined,
    shareMsg: undefined,
    subsidiesMoney: undefined,
    subsidiesStatus: undefined,
    subsidiesMsg: undefined,
    subsidyId: undefined,
    params: {
    }
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询球星卡订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOrder(queryParams.value);
  orderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  orderFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: OrderVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加球星卡订单";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: OrderVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getOrder(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改球星卡订单";
}

/** 提交按钮 */
const submitForm = () => {
  orderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateOrder(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addOrder(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

const handleView = (row?: OrderVO) => {
  cardOrderId.value = row?.id;
  orderDialog.visible = true;
}

const viewListCarmi = (row?: OrderVO, info) => {
  cardOrderId.value = row?.id;
  cardCollageId.value = row?.cardCollageId;
  carmiDialog.visible = true;
  carmiDialog.title = info;
}

const viewExclusIveListCarmi = (row?: OrderVO) => {
  cardOrderId.value = row?.id;
  cardCollageId.value = row?.cardCollageId;
  exclusiveCarmiDialog.visible = true;
  exclusiveCarmiDialog.title = '专属查看';
}

/** 删除按钮操作 */
const handleDelete = async (row?: OrderVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除球星卡订单编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delOrder(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
}

const props = defineProps({
  cardCollageId: {
    type: String,
    default: ''
  },
  collageCode: {
    type: String,
    default: ''
  },
  shopName: {
    type: String,
    default: ''
  },
});

watch(
  () => props.cardCollageId,
  (val) => {
    queryParams.value.cardCollageId = props.cardCollageId;
    queryParams.value.collageCode = props.collageCode;
    queryParams.value.shopName = props.shopName;
    getList();
  },
  { immediate: true, deep: true } // 立即执行一次回调
);
</script>

<style lang="scss" scoped>
.contentItem {
 display: flex;
 align-items: center;

 span {
   display: inline-block;
 }

 :first-child {
   width: 40%;
   // text-align: right;
 }

 :last-child {
   padding-left: 4px;
   // text-align: left;
   width: 60%;
   white-space: nowrap;
   overflow: hidden;
   text-overflow: ellipsis;
 }

 .inlineBlockItem {
   div {
     display: inline-block;
   }
 }
}
.contentTitle {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .titleText {
    width: 90px;
    font-size: 18px;
  }

  .lineBox {
    width: 100%;

    .el-divider {
      height: 1.5px;
    }
  }
}

::v-deep .el-scrollbar .el-scrollbar__wrap {
  overflow-x: hidden;
}

::v-deep .el-scrollbar__bar.is-horizontal {
  display: none;
}

::v-deep .formContent {
  .el-input-number {
    width: 100%;
  }

  .el-icon-arrow-up {
    display: none;
  }

  .el-input__inner {
    border: unset;
  }

  .el-input {
    width: 100%;
  }

  .el-form-item__error {
    width: 200%;
    text-align: left;
  }

  .el-input.is-disabled .el-input__inner {
    background-color: #FFFFFF;
    color: #606266;
    cursor: unset;
  }

  .is-disabled {
    background: unset;
    color: unset;
    background-color: unset;
  }

  .el-date-editor {
    width: 100%;
  }

  .contentTitle {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .titleText {
      width: 90px;
      font-size: 18px;
    }

    .lineBox {
      width: 100%;

      .el-divider {
        height: 1.5px;
      }
    }
  }

  .formTable {
    .el-table--medium .el-table__cell {
      padding: 10px 0 0;
    }

    .el-form-item {
      margin-top: 3px;
      margin-bottom: 14px !important;

      .el-form-item__error {
        padding-top: unset;
      }
    }
  }
}

::v-deep .el-table .hidden-row {
  display: none;
}

.main-order-header {
  margin-top: 10px;
  display: flex;
  height: 45px;
  background-color: #F0F0F0;
  border: 1px solid #BBBBBB;
  line-height: 40px;
  font-size: 15px;
  color: gray;
  text-align: center;
  flex-direction: row
}

::v-deep .el-table__header {
  .el-table-column--selection {
    .cell {
      .el-checkbox:after {
        content: "全选";
        word-break: break-word;
        color: #515a6e;
        font-weight: bold;
        margin-left: 12px;
        font-size: 13px;
      }
    }
  }
}
::v-deep .queryOrderCarmi{
  .el-input {
    width: 100%;
  }
}
</style>
